$(function () {
	// 自动登录
	$("span.radio-box").click(function () {
		$(this).find("img").toggleClass("isCheck");
		$("input[name='remember-me']").prop("checked",!$("input[name='remember-me']").prop("checked"));
	});
	// 切换登录方式
	$(".btn-box button").click(function () {
		let btnId = $(this).data("id");
		// console.log(btnId);
		if (btnId == 0 || btnId == 1) {
			$(this).fadeOut(100, function () {
				$(this)
					.addClass("btn-active")
					.siblings(".phone-login")
					.fadeIn(500)
					.removeClass("btn-active");
			});

			$("#form_key").fadeOut(100, function () {
				let other_way = $("#form_key .form-item-box .others-way");
				let form_h3 = $("#form_key .form-item-box h3");
				if (btnId == 1) {
					other_way.css("padding", "40px 0");
					form_h3.text("手机号登录");
					$(this).attr("action","/auth/mobile/form")
				} else {
					other_way.css("padding", "0");
					form_h3.text("账号密码登录");
					$(this).attr("action","/auth/login/form")
				}
				$("#form_key .form-item-box .form-item.form-bottom").toggleClass(
					"form-active"
				);
				$(this).fadeIn(900);
			});
		}
	});
	// 去注册
	$("button.register").click(function () {
		$("div.tip-left")
			.addClass("tip-active")
			.siblings()
			.removeClass("tip-active");
		$("#form_key").animate(
			{
				left: "65px",
				height: "608px",
				opacity: "0.1",
			},
			"slow",
			"swing",
			function () {
				$(this)
					.css({ left: "auto", height: "auto", opacity: "1" })
					.addClass("active")
					.siblings()
					.css({ opacity: "0.1" })
					.removeClass("active")
					.animate(
						{
							opacity: "1",
						},
						"slow",
						"swing"
					);
			}
		);
	});
	// 去登录
	$("button.login").click(function () {
		$("div.tip-right")
			.addClass("tip-active")
			.siblings()
			.removeClass("tip-active");
		$("#form_register").animate(
			{
				left: "425px",
				height: "560px",
				opacity: "0.1",
			},
			"slow",
			"swing",
			function () {
				$(this)
					.css({ left: "65px", height: "auto", opacity: "1" })
					.addClass("active")
					.siblings()
					.css({ opacity: "0.1" })
					.removeClass("active")
					.animate(
						{
							opacity: "1",
						},
						"slow",
						"swing"
					);
			}
		);
	});
	// 获取验证码
	let count = 60,
		timer = null;
	function getCode(dom) {
		if (count == 0) {
			count = 60;
			clearInterval(timer);
			$(dom)
				.text("重新发送")
				.css({ color: "#3d7eff", cursor: "pointer" })
				.attr("disabled", false);
			return;
		} else {
			count--;
			$(dom)
				.text(count + "秒后再重发")
				.attr("disabled", true)
				.css({ color: "#999", cursor: "default" });
		}
		timer = setTimeout(function(){
			getCode(dom);
		}, 1000);
	}

	$("#form_key .input-right.get-code").click(function (e) {
		e.preventDefault();
		if(count == 60){
			var pval = $("#form_key .input.phoneInput").val();
			if (!pval){
				alert("请输入手机号")
				return ;
			}
			var url = "/auth/code/mobile?mobile=" + pval;
			var _this = $(this);
			$.get(url, function(data){
				alert(data.code === 200 ? "发送成功": "发送失败");
				if (data.code === 200){
					getCode(_this);
				}
			});
		}
	});
	$(".input-code").click(function (e) {
		$(".input-code img").attr("src","../code/image?t="+Math.random());
	})
	$("#form_register .input-right.get-code").click(function (e) {
		e.preventDefault();
		var pval = $("#form_register input[name='mobile']").val();
		var url = "/auth/register/code/mobile?mobile=" + pval;
		var _this = $(this);
		$.get(url, function(data){
			alert(data.code === 200 ? "发送成功": "发送失败");
			if (data.code === 200){
				getCode(_this);
			}
		});
	});
	$("#register").click(function (e) {
		let fields=$("#form_register").serializeArray();
		var obj = {}; //声明一个对象
		$.each(fields, function(index, field) {
			obj[field.name] = field.value; //通过变量，将属性值，属性一起放到对象中
		})
		$.ajax({
			url: "/auth/sys/user/register",
			type: "POST",
			data: JSON.stringify(obj),
			dataType: "json",
			async: false,
			contentType:'application/json;charset=UTF-8',  //设置后
			success: function (data) {
				if(data.code === 200 )
				{
					alert("注册成功");
				}else
					alert("注册失败:"+data.message);
			},
			error: function () {
				alert("注册失败！");
			}
		});
	})


	// 校验

	Focuss($(".userInput"));
	Focuss($(".pwdInput"));
	Focuss($(".pwd1Input"));
	Focuss($(".phoneInput"));
	Focuss($(".emailInput"));
	Focuss($(".inputCode"));

	Blurr($(".userInput"), "请输入用户名");
	Blurr($(".pwdInput"), "请输入登录密码");
	Blurr($(".pwd1Input"), "请确认密码");
	Blurr($(".phoneInput"), "请输入您的手机号");
	Blurr($(".emailInput"), "请输入您的邮箱");
	Blurr($(".inputCode"), "请输入验证码");

	function Focuss(ele) {
		ele.focus(function (e) {
			e.preventDefault();
			_this = $(this);
			_this.parent().removeClass("isShow").removeAttr("placeholder");
		});
	}

	function Blurr(eleb, contentb) {
		eleb.blur(function (e) {
			e.preventDefault();
			_this = $(this);
			if (
				_this.val() == null ||
				_this.val() == "" ||
				_this.val() == undefined
			) {
				_this.parent().addClass("isShow").attr("placeholder", contentb);
			} else {
				_this.parent().removeClass("isShow").removeAttr("placeholder");
			}
		});
	}
});
